package com.example.lenovo.weiyi;

import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.com.ruanmeng.demon.ServiceXQM;
import com.com.ruanmeng.utils.ImgDataUtil;
import com.com.ruanmeng.utils.PreferencesUtils;
import com.com.ruanmeng.view.MyListView;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.rest.Request;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import nohttp.CallServer;
import nohttp.CustomHttpListener;
import share.Const;
import share.HttpIP;
import yulanpic.ImagePagerActivity;

/**
 * 服务详情
 */
public class ServiceXQActivity extends BaseActivity {

    @BindView(R.id.tv_zs)
    TextView tvZs;
    @BindView(R.id.tv_service_jc)
    TextView tvServiceJc;
    @BindView(R.id.tv_service_pg)
    TextView tvServicePg;
    @BindView(R.id.tv_service_xg)
    TextView tvServiceXg;
    @BindView(R.id.tv_service_jy)
    TextView tvServiceJy;
    @BindView(R.id.mlist_service_pic)
    MyListView mlistServicePic;
    @BindView(R.id.tv_service_bz)
    TextView tvServiceBz;
    ArrayList<ServiceXQM.OtoAdvicesBean> Temp_listpic = new ArrayList<>();
    @BindView(R.id.tv_service_zl)
    TextView tvServiceZl;
    @BindView(R.id.li_service_pic)
    LinearLayout liServicePic;
    private PicAdapter picadapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_service_xq);
        ButterKnife.bind(this);
        ChangLayTitle("服务报告");
        LayBack();
        init();
        getData();
    }

    public void init() {
        //图片
        picadapter = new PicAdapter(Temp_listpic);
        mlistServicePic.setAdapter(picadapter);
    }

    public void getData() {
        Request<String> mRequest = NoHttp.createStringRequest(HttpIP.Service_detail, Const.POST);
        mRequest.addHeader("token", PreferencesUtils.getString(ServiceXQActivity.this, "uid"));
//        mRequest.add("otoRecordId", "900e99b215e14d428867c7b8a0fff0e4");
        mRequest.add("otoRecordId", getIntent().getStringExtra("aid"));
        // 添加到请求队列
        CallServer.getRequestInstance().add(ServiceXQActivity.this, 0, mRequest,
                new CustomHttpListener(ServiceXQActivity.this, true, ServiceXQM.class) {
                    @Override
                    public void doWork(Object data, boolean isOne) {
                        ServiceXQM model = ((ServiceXQM) data);
                        tvZs.setText(model.getZhusu());
                        tvServiceJc.setText(model.getJiancha());
                        tvServicePg.setText(model.getPingjia());
                        tvServiceZl.setText(model.getZhiliao());
                        tvServiceXg.setText(model.getXiaoguo());
                        tvServiceJy.setText(model.getZhiliaoText());//建议
                        if(model.getOtoAdvices()!=null){
                            Temp_listpic.addAll(model.getOtoAdvices());//图片
                            picadapter.notifyDataSetChanged();
                        }
                        tvServiceBz.setText(model.getRemark());
                        if (Temp_listpic.size() == 0) {
                            liServicePic.setVisibility(View.GONE);
                        }
                    }

                    @Override
                    public void onFinally(JSONObject obj) throws JSONException {
                        super.onFinally(obj);
                    }
                }, true, true);
    }


    /**
     * 图片
     */
    class PicAdapter extends BaseAdapter {
        private List<ServiceXQM.OtoAdvicesBean> list;

        public PicAdapter(List<ServiceXQM.OtoAdvicesBean> list) {
            this.list = list;
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View v, ViewGroup parent) {
            v = getLayoutInflater().inflate(R.layout.item_zhidao, null);
            ImageView imv_action_pic = (ImageView) v.findViewById(R.id.imv_action_pic);
            ImageView imv_zdyj = (ImageView) v.findViewById(R.id.imv_zdyj);
            imv_action_pic.setVisibility(View.VISIBLE);
            TextView tv_action_content = (TextView) v.findViewById(R.id.tv_action_content);
            View view_ban = (View) v.findViewById(R.id.view_action_ban);
            View view_man = (View) v.findViewById(R.id.view_action_man);
            //语音
            LinearLayout li_action_play = (LinearLayout) v.findViewById(R.id.li_action_play);
            ImageView imv_action_play = (ImageView) v.findViewById(R.id.imv_action_play);
            ImgDataUtil.loadYuanImage(ServiceXQActivity.this, HttpIP.Base_IpIMage + list.get(position).getAdvicePic(), imv_zdyj);
            tv_action_content.setMovementMethod(LinkMovementMethod.getInstance());
            tv_action_content.setText(Html.fromHtml(list.get(position).getAdviceContent()).toString());
            li_action_play.setVisibility(View.VISIBLE);
            if (position == Temp_listpic.size() - 1) {
                view_ban.setVisibility(View.GONE);
                view_man.setVisibility(View.GONE);
            } else {
                view_ban.setVisibility(View.VISIBLE);
                view_man.setVisibility(View.GONE);
            }
            imv_action_pic.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(ServiceXQActivity.this, VideoXQActivity.class);
                    intent.putExtra("title","");
                    intent.putExtra("intro",Temp_listpic.get(position).getAdviceContent());
                    intent.putExtra("link",Temp_listpic.get(position).getAdvicePic());
                    startActivity(intent);
//                    YuLanPic(HttpIP.Base_IpIMage + Temp_listpic.get(position).getAdvicePic());
                }
            });
            return v;
        }

    }

    /**
     * 预览图片
     */
    public void YuLanPic(String url) {
        String[] aStrings = new String[1];
        aStrings[0] = url;
        Intent intent1 = new Intent(ServiceXQActivity.this,
                ImagePagerActivity.class); //
        // 图片url,为了演示这里使用常量，一般从数据库中或网络中获取
        intent1.putExtra(ImagePagerActivity.EXTRA_IMAGE_URLS, aStrings);
        intent1.putExtra(ImagePagerActivity.EXTRA_IMAGE_INDEX, 0);
        startActivity(intent1);
    }
}
